package com.management.demo.controller.mall;

import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.management.demo.common.dto.PageDTO;
import com.management.demo.common.vo.PageVO;
import com.management.demo.domain.mall.PmsBrand;
import com.management.demo.dto.mall.PmsBrandDTO;
import com.management.demo.dto.mall.PmsBrandQueryDTO;
import com.management.demo.service.mall.PmsBrandService;
import com.management.demo.vo.mall.PmsBrandVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/pms/brand")
@Tag(name = "品牌管理", description = "品牌相关接口")
public class PmsBrandController {

    @Autowired
    private PmsBrandService pmsBrandService;

    @Operation(summary = "分页查询")
    @PostMapping("page")
    public SaResult page(@RequestBody PmsBrandQueryDTO pmsBrandQueryDTO) {
        String name = pmsBrandQueryDTO.getName();
        Integer currentPage = pmsBrandQueryDTO.getCurrentPage();
        Integer pageSize = pmsBrandQueryDTO.getPageSize();

        LambdaQueryWrapper<PmsBrand> queryWrapper = new LambdaQueryWrapper<PmsBrand>()
                .like(StringUtils.isNotBlank(name), PmsBrand::getName, name);
        Page<PmsBrand> pmsBrandPage = pmsBrandService.page(new Page<>(currentPage, pageSize), queryWrapper);

        PageVO<PmsBrandVO> pmsBrandPageVO = PageDTO.getPageVO(pmsBrandPage, item -> BeanUtil.copyProperties(item, PmsBrandVO.class));
        return SaResult.ok().setData(pmsBrandPageVO);
    }

    @Operation(summary = "查询所有")
    @GetMapping("list")
    public SaResult list() {
        List<PmsBrand> pmsBrands = pmsBrandService.list();
        return SaResult.ok().setData(BeanUtil.copyToList(pmsBrands, PmsBrandVO.class));
    }

    @Operation(summary = "新增/修改")
    @PostMapping("createOrEdit")
    public SaResult createOrEdit(@RequestBody PmsBrandDTO pmsBrandDTO) {
        PmsBrand pmsBrand = BeanUtil.copyProperties(pmsBrandDTO, PmsBrand.class);
        boolean result = pmsBrandService.saveOrUpdate(pmsBrand);
        return SaResult.ok().setData(result);
    }

    @Operation(summary = "根据id获取信息")
    @GetMapping("getById/{id}")
    public SaResult getById(@Parameter(description = "品牌id") @PathVariable Long id) {
        PmsBrand pmsBrand = pmsBrandService.getById(id);
        return SaResult.ok().setData(BeanUtil.copyProperties(pmsBrand, PmsBrandVO.class));
    }

    @Operation(summary = "删除")
    @DeleteMapping("remove/{id}")
    public SaResult remove(@Parameter(description = "id") @PathVariable Long id) {
        boolean result = pmsBrandService.removeById(id);
        return SaResult.ok().setData(result);
    }

    @Operation(summary = "批量删除")
    @DeleteMapping("batchRemove")
    public SaResult remove(@Parameter(description = "id列表") @RequestBody List<Integer> idList) {
        boolean result = pmsBrandService.removeByIds(idList);
        return SaResult.ok().setData(result);
    }

    @Operation(summary = "修改展示状态")
    @PostMapping("changeStatus/{id}/{status}")
    public SaResult changeStatus(@Parameter(description = "id") @PathVariable Integer id,
                                 @Parameter(description = "是否展示") @PathVariable Integer status) {
        UpdateWrapper<PmsBrand> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("status", status);
        updateWrapper.eq("id", id);
        boolean result = pmsBrandService.update(updateWrapper);
        return SaResult.ok().setData(result);
    }
}
